Upgrading Session File Storage in Legacy apps

Description

Prior to Alpha Five Version 11, developers often used the Session.Session_Folder property as a location in which to save session files and Session.Session_URL as a base URL to then link to the session file. Both of these properties were removed in Version 11, which means legacy code which uses them must be updated.

This article applies to developers who are migrating applications from Alpha Five V10 or older to a newer release of Alpha Anywhere.

Files are now stored in sessions using the methods described below:

  • Session.SaveDataAsFile() - saves data directly to the session storage as opposed to being a string value like Session.Session_Folder which could be used as part of a file path.

  • Session.FormatFileDataURL() - is similar to using Session.Session_URL.

  • Session.SaveSessionFileToFile() - This method allows you to save a file in the session folder to another location on the server. The syntax for this method is session.saveSessionFileToFile(c Key, C DestinationFile). The session file may have been created using either the session.saveFileToSessionFile( c fileName, c key) or session.saveDataAsFile(b data, c key) methods.

Typically, Session.Session_Folder and Session.Session_URL have been used in a pattern such as that shown below.

<%a5
dim Filename as c = "MyFile.txt"
save_to_file("Hello World, this is a session file",Session.Session_Folder + Filename)
Response.Redirect(Session.Session_Url + Filename)
%>

The legacy code below can be directly replaced by the following code:

<%a5
dim FileKey as c = "MyFile.txt"
Session.SaveDataAsFile(FileKey,"Hello World, this is a session file")
Response.Redirect(Session.FormatFileDataURL(FileKey))
%>

Another common usage of these properties prior to Version 11 is for generation of reports as a session file:

<%a5
dim filter as c = ""
dim order as c = ""
Dim filename as c = Session.Session_Folder + "tempreport.pdf"
filename = report.saveas("MyReport@[PathAlias.ADB_Path]\MyApp.ddd","pdf",filter,order,filename,.f.)
if file.exists(filename)
    Response.Redirect(Session.Session_URL + "tempreport.pdf")
end if
%>

This example is a bit more involved to replace because there is no direct replacement for the Session.Session_Folder property in order to build a file name to be used by Report.SaveAs(). Because of this, Request.GetRequestTempFileName() should be used to provide a temporary file location for the report generation, and then the file's contents can be moved into session storage.

<%a5
dim filter as c = ""
dim order as c = ""
Dim filename as c = Request.GetRequestTempFileName()
filename = report.saveas("MyReport@[PathAlias.ADB_Path]\MyApp.ddd","pdf",filter,order,filename,.f.)
if file.exists(filename)
    Session.SaveDataAsFile("tempreport.pdf",file.to_blob(filename))
    Response.Redirect( Session.FormatFileDataURL("tempreport.pdf") )
end if
%>

The code above uses Session.SaveDataAsFile() and Response.Redirect() in order to demonstrate the usage of those methods. However, this is NOT the most efficient way of sending the PDF contents back to the browser. Using Response.SendFile() is a Best Practice and is strongly recommended instead, as shown below:

<%a5
dim filter as c = ""
dim order as c = ""
Dim filename as c = Request.GetRequestTempFileName()
filename = report.saveas("MyReport@[PathAlias.ADB_Path]\MyApp.ddd","pdf",filter,order,filename,.f.)
if file.exists(filename)
    Response.SendFile(filename)
end if
%>

See Also